package org.jeecg.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class KyTT {


    @Test
    public void syncDepartData() throws JsonProcessingException {

        String jsonString = "{\n" +
                "    \"code\": 200,\n" +
                "    \"message\": \"获取科研项目成功!\",\n" +
                "    \"data\": \"[{\\\"ProjectId\\\":3,\\\"ProjectCode\\\":\\\"地方\\\",\\\"ProjectName\\\":\\\"会更好\\\",\\\"ProjectContent\\\":\\\"共和国\\\",\\\"ProjectLevel\\\":\\\"group\\\",\\\"ProjectAttribute\\\":\\\"project\\\",\\\"ProjectType\\\":\\\"major\\\",\\\"ProfessionType\\\":\\\"cljs\\\",\\\"InchargeUnit\\\":8,\\\"ProjectManager\\\":449,\\\"ProjectMembers\\\":\\\"\\\",\\\"StartDate\\\":\\\"2023-11-10T00:00:00\\\",\\\"EndDate\\\":\\\"2023-11-14T00:00:00\\\",\\\"StudyDomain\\\":\\\"\\\",\\\"Kpi\\\":\\\"\\\",\\\"IpOutcome\\\":\\\"\\\",\\\"Remark\\\":\\\"\\\",\\\"Status\\\":\\\"active\\\",\\\"IsDeleted\\\":0,\\\"CreateUser\\\":38,\\\"CreateTime\\\":\\\"2023-11-10T14:15:32.1\\\"},{\\\"ProjectId\\\":33,\\\"ProjectCode\\\":\\\"1\\\",\\\"ProjectName\\\":\\\"1111\\\",\\\"ParentId\\\":1,\\\"ProjectContent\\\":\\\"111111\\\",\\\"ProjectLevel\\\":\\\"national\\\",\\\"ProjectAttribute\\\":\\\"project\\\",\\\"ProjectType\\\":\\\"research\\\",\\\"ProfessionType\\\":\\\"cljs\\\",\\\"InchargeUnit\\\":5,\\\"ProjectManager\\\":449,\\\"ProjectMembers\\\":\\\"\\\",\\\"StartDate\\\":\\\"2023-11-11T00:00:00\\\",\\\"EndDate\\\":\\\"2023-11-15T00:00:00\\\",\\\"StudyDomain\\\":\\\"\\\",\\\"Kpi\\\":\\\"\\\",\\\"IpOutcome\\\":\\\"\\\",\\\"Remark\\\":\\\"\\\",\\\"Status\\\":\\\"active\\\",\\\"IsDeleted\\\":0,\\\"CreateUser\\\":36,\\\"CreateTime\\\":\\\"2023-11-11T11:09:03.223\\\"}]\"\n" +
                "}";
        // 创建ObjectMapper对象

//        JSON parse = (JSON) JSON.parse(jsonString);
//        List list = parse.toJavaObject(List.class);
        ObjectMapper mapper = new ObjectMapper();

        // 将字符串转换为JsonNode对象
        JsonNode jsonNode = mapper.readTree(jsonString);

        // 获取code属性的值
        // int code = jsonNode.get("code").asInt();
        // 获取msg属性的值
        // String msg = jsonNode.get("msg").asText();

        // 获取data属性的值
        String dataStr = jsonNode.get("data").toString();
        System.out.println(dataStr);
        JSONArray dataNode = JSON.parseArray(dataStr);
        System.out.println(dataNode.size());
            for (int i = 0; i < dataNode.size(); i++) {
                JSONObject jsonObject = dataNode.getJSONObject(i);
                int projectId = jsonObject.getIntValue("ProjectId");
                String projectName = jsonObject.getString("ProjectName");
                String projectContent = jsonObject.getString("ProjectContent");
                System.out.println(projectId);
                System.out.println(projectName);
                System.out.println(projectContent);
            }
        }


        @Test
    public void sysKYSJ() {
//        String jsonString = "{\n" +
//                "    \"code\": 200,\n" +
//                "    \"message\": \"获取科研项目成功!\",\n" +
//                "    \"data\": \"[{\\\"ProjectId\\\":3,\\\"ProjectCode\\\":\\\"地方\\\",\\\"ProjectName\\\":\\\"会更好\\\",\\\"ProjectContent\\\":\\\"共和国\\\",\\\"ProjectLevel\\\":\\\"group\\\",\\\"ProjectAttribute\\\":\\\"project\\\",\\\"ProjectType\\\":\\\"major\\\",\\\"ProfessionType\\\":\\\"cljs\\\",\\\"InchargeUnit\\\":8,\\\"ProjectManager\\\":449,\\\"ProjectMembers\\\":\\\"\\\",\\\"StartDate\\\":\\\"2023-11-10T00:00:00\\\",\\\"EndDate\\\":\\\"2023-11-14T00:00:00\\\",\\\"StudyDomain\\\":\\\"\\\",\\\"Kpi\\\":\\\"\\\",\\\"IpOutcome\\\":\\\"\\\",\\\"Remark\\\":\\\"\\\",\\\"Status\\\":\\\"active\\\",\\\"IsDeleted\\\":0,\\\"CreateUser\\\":38,\\\"CreateTime\\\":\\\"2023-11-10T14:15:32.1\\\"},{\\\"ProjectId\\\":33,\\\"ProjectCode\\\":\\\"1\\\",\\\"ProjectName\\\":\\\"1111\\\",\\\"ParentId\\\":1,\\\"ProjectContent\\\":\\\"111111\\\",\\\"ProjectLevel\\\":\\\"national\\\",\\\"ProjectAttribute\\\":\\\"project\\\",\\\"ProjectType\\\":\\\"research\\\",\\\"ProfessionType\\\":\\\"cljs\\\",\\\"InchargeUnit\\\":5,\\\"ProjectManager\\\":449,\\\"ProjectMembers\\\":\\\"\\\",\\\"StartDate\\\":\\\"2023-11-11T00:00:00\\\",\\\"EndDate\\\":\\\"2023-11-15T00:00:00\\\",\\\"StudyDomain\\\":\\\"\\\",\\\"Kpi\\\":\\\"\\\",\\\"IpOutcome\\\":\\\"\\\",\\\"Remark\\\":\\\"\\\",\\\"Status\\\":\\\"active\\\",\\\"IsDeleted\\\":0,\\\"CreateUser\\\":36,\\\"CreateTime\\\":\\\"2023-11-11T11:09:03.223\\\"}]\"\n" +
//                "}";

            String jsonString ="{\n" +
                    "    \"code\": 200,\n" +
                    "    \"message\": \"获取科研项目成功!\",\n" +
                    "    \"data\": \"[{\\\"ProjectId\\\":3,\\\"ProjectCode\\\":\\\"地方\\\",\\\"ProjectName\\\":\\\"会更好\\\",\\\"ProjectContent\\\":\\\"共和国\\\",\\\"ProjectLevel\\\":\\\"group\\\",\\\"ProjectAttribute\\\":\\\"project\\\",\\\"ProjectType\\\":\\\"major\\\",\\\"ProfessionType\\\":\\\"cljs\\\",\\\"InchargeUnit\\\":8,\\\"ProjectManager\\\":449,\\\"ProjectMembers\\\":\\\"\\\",\\\"StartDate\\\":\\\"2023-11-10T00:00:00\\\",\\\"EndDate\\\":\\\"2023-11-14T00:00:00\\\",\\\"StudyDomain\\\":\\\"\\\",\\\"Kpi\\\":\\\"\\\",\\\"IpOutcome\\\":\\\"\\\",\\\"Remark\\\":\\\"\\\",\\\"Status\\\":\\\"active\\\",\\\"IsDeleted\\\":0,\\\"CreateUser\\\":38,\\\"CreateTime\\\":\\\"2023-11-10T14:15:32.1\\\"},{\\\"ProjectId\\\":33,\\\"ProjectCode\\\":\\\"1\\\",\\\"ProjectName\\\":\\\"1111\\\",\\\"ParentId\\\":1,\\\"ProjectContent\\\":\\\"111111\\\",\\\"ProjectLevel\\\":\\\"national\\\",\\\"ProjectAttribute\\\":\\\"project\\\",\\\"ProjectType\\\":\\\"research\\\",\\\"ProfessionType\\\":\\\"cljs\\\",\\\"InchargeUnit\\\":5,\\\"ProjectManager\\\":449,\\\"ProjectMembers\\\":\\\"\\\",\\\"StartDate\\\":\\\"2023-11-11T00:00:00\\\",\\\"EndDate\\\":\\\"2023-11-15T00:00:00\\\",\\\"StudyDomain\\\":\\\"\\\",\\\"Kpi\\\":\\\"\\\",\\\"IpOutcome\\\":\\\"\\\",\\\"Remark\\\":\\\"\\\",\\\"Status\\\":\\\"active\\\",\\\"IsDeleted\\\":0,\\\"CreateUser\\\":36,\\\"CreateTime\\\":\\\"2023-11-11T11:09:03.223\\\"}]\"\n" +
                    "}";

        // 解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(jsonString);

        // 获取code属性的值
        int code = jsonObject.getIntValue("code");
        System.out.println("code: " + code);

        // 获取message属性的值
        String message = jsonObject.getString("message");
        System.out.println("message: " + message);

        // 获取data属性的值
        String dataString = jsonObject.getString("data");

        // 解析data属性的值为JSONArray
        JSONArray dataArray = JSON.parseArray(dataString);

        // 遍历数组获取每个项目的属性值
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject projectObject = dataArray.getJSONObject(i);

            String projectId = projectObject.getString("ProjectId");
            String projectCode = projectObject.getString("ProjectCode");
            String projectName = projectObject.getString("ProjectName");
            String ProjectContent = projectObject.getString("ProjectContent");
            String ProjectManager = projectObject.getString("ProjectManager");
            String StartDate = projectObject.getString("StartDate");
            String EndDate = projectObject.getString("EndDate");
        }
    }
}
